package petrochina.company.electric.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.toast.ToastUtils;
import com.tencent.mmkv.MMKV;

import org.greenrobot.eventbus.EventBus;

import petrochina.company.electric.R;
import petrochina.company.electric.app.AppActivity;
import petrochina.company.electric.event.ManagerHandleTaskResultEvent;
import petrochina.company.electric.http.api.LoginApi;
import petrochina.company.electric.model.ArtificialRequestData;

/**
 * desc   : 消息处理弹框页
 */
public final class MessageDialogActivity extends AppActivity {
    public static final String INTENT_KEY_DATA_JSON = "data_json";

    private TextView tvMessageTitle, tvMessageContent, tvMessageUser, tvMessageClass, tvMessageTask;
    private AppCompatButton btnCancel, btnConfirm;

    private MMKV mmkv;
    private Gson gson;
    private LoginApi.Bean mLogin;
    private String dataJson;
    private ArtificialRequestData mData;

    public static void start(Context context, String data) {
        Intent intent = new Intent(context, MessageDialogActivity.class);
        intent.putExtra(INTENT_KEY_DATA_JSON, data);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.message_dialog_activity;
    }

    @Override
    protected void initView() {
        mmkv = MMKV.defaultMMKV();
        gson = new Gson();
        tvMessageTitle = findViewById(R.id.tv_message_title);
        tvMessageContent = findViewById(R.id.tv_message_content);
        tvMessageUser = findViewById(R.id.tv_message_user);
        tvMessageClass = findViewById(R.id.tv_message_class);
        tvMessageTask = findViewById(R.id.tv_message_task);
        btnCancel = findViewById(R.id.btn_cancel);
        btnConfirm = findViewById(R.id.btn_confirm);
        setOnClickListener(btnCancel, btnConfirm);
    }

    @Override
    protected void initData() {
        dataJson = getString(INTENT_KEY_DATA_JSON);
        String loginJson = mmkv.getString("loginData", null);
        if (!TextUtils.isEmpty(loginJson)) {
            mLogin = gson.fromJson(loginJson, LoginApi.Bean.class);
        }

        if (!TextUtils.isEmpty(dataJson)) {
            try {
                mData = new Gson().fromJson(dataJson, ArtificialRequestData.class);
            } catch (JsonSyntaxException e) {

            }
        }
        if (mLogin == null || mData == null) {
            ToastUtils.show("数据异常");
            finish();
        }
        tvMessageTitle.setText("人工确认");
        if (mData.getLogin() != null) {
            tvMessageUser.setText("操作人：" + mData.getLogin().getStaffName());
            tvMessageClass.setText("所属：" + mData.getLogin().getStaffGroupName());
        }
        if (mData.getTask() != null) {
            tvMessageTask.setText(mData.getTask().getName());
        }
        if (mData.getDetail() != null) {
            tvMessageContent.setText(mData.getDetail().getEquipName() + "需要进行人工确认！");
        }
//        // 刷新用户信息
//        EasyHttp.post(this)
//                .api(new UserInfoApi())
//                .request(new HttpCallback<HttpData<UserInfoApi.Bean>>(this) {
//
//                    @Override
//                    public void onSucceed(HttpData<UserInfoApi.Bean> data) {
//
//                    }
//                });
    }

    @NonNull
    @Override
    protected ImmersionBar createStatusBarConfig() {
        return super.createStatusBarConfig()
                // 隐藏状态栏和导航栏
                .hideBar(BarHide.FLAG_HIDE_BAR);
    }

    @Override
    public void onBackPressed() {
        //禁用返回键
        //super.onBackPressed();
    }

    @Override
    protected void initActivity() {
        // 问题及方案：https://www.cnblogs.com/net168/p/5722752.html
        // 如果当前 Activity 不是任务栈中的第一个 Activity
        if (!isTaskRoot()) {
            Intent intent = getIntent();
            // 如果当前 Activity 是通过桌面图标启动进入的
            if (intent != null && intent.hasCategory(Intent.CATEGORY_LAUNCHER)
                    && Intent.ACTION_MAIN.equals(intent.getAction())) {
                // 对当前 Activity 执行销毁操作，避免重复实例化入口
                finish();
                return;
            }
        }
        super.initActivity();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id = view.getId();
        if (id == R.id.btn_cancel) {
            ToastUtils.show("点击不通过");
            ManagerHandleTaskResultEvent event = new ManagerHandleTaskResultEvent(2, mLogin.getId(), mData.getLogin().getId(), "");
            EventBus.getDefault().post(event);
            finish();
        } else if (id == R.id.btn_confirm) {
            ToastUtils.show("点击通过");
            ManagerHandleTaskResultEvent event = new ManagerHandleTaskResultEvent(3, mLogin.getId(), mData.getLogin().getId(), "");
            EventBus.getDefault().post(event);
            finish();
        }
    }

    @Deprecated
    @Override
    protected void onDestroy() {
        // 因为修复了一个启动页被重复启动的问题，所以有可能 Activity 还没有初始化完成就已经销毁了
        // 所以如果需要在此处释放对象资源需要先对这个对象进行判空，否则可能会导致空指针异常
        super.onDestroy();
    }
}